Skip to content

ErrorMessageBox

Base classes

Name Children Inherits
MessageBox
prettyqt.widgets.messagebox
Modal dialog for informing the user (and for receiving an answer).

⋔ Inheritance diagram

graph TD
  1198304725["debugging.ErrorMessageBox"]
  3869923910["widgets.MessageBox"]
  2345124717["widgets.DialogMixin"]
  829357528["widgets.WidgetMixin"]
  3227531497["core.ObjectMixin"]
  2751680898["builtins.object"]
  625385617["gui.PaintDeviceMixin"]
  1716705937["QtWidgets.QMessageBox"]
  392105360["QtWidgets.QDialog"]
  3620195106["QtWidgets.QWidget"]
  3984060476["QtCore.QObject"]
  37984305["Shiboken.Object"]
  673956333["QtGui.QPaintDevice"]
  3869923910 --> 1198304725
  2345124717 --> 3869923910
  829357528 --> 2345124717
  3227531497 --> 829357528
  2751680898 --> 3227531497
  625385617 --> 829357528
  2751680898 --> 625385617
  2751680898 --> 829357528
  2751680898 --> 2345124717
  1716705937 --> 3869923910
  392105360 --> 1716705937
  3620195106 --> 392105360
  3984060476 --> 3620195106
  37984305 --> 3984060476
  2751680898 --> 37984305
  673956333 --> 3620195106
  37984305 --> 673956333

🛈 DocStrings

Bases: MessageBox

An message box widget for displaying Python exception.

Source code in prettyqt/debugging/errormessagebox.py
class ErrorMessageBox(widgets.MessageBox):
    """An message box widget for displaying Python exception."""

    def __init__(self, title: str, text_or_exception: str | Exception, parent=None):
        if isinstance(text_or_exception, str):
            text = text_or_exception
            exc = None
        else:
            text = str(text_or_exception)
            exc = text_or_exception
        super().__init__(
            # icon=MBox.Icon.Critical,
            window_title=title,
            text=str(text)[:1000],
            buttons=["ok", "help", "close"],
            parent=parent,
        )

        self._exc = exc

        traceback_button = self.get_button("help")
        traceback_button.setText("Show traceback")
        close_button = self.get_button("close")
        close_button.setText("Quit application")

    def exec(self):
        match super().exec():
            case widgets.MessageBox.StandardButton.Help:
                tb = self._get_traceback()
                dlg = debugging.TracebackDialog(self)
                dlg.setText(tb)
                dlg.exec()
                return None
            case widgets.MessageBox.StandardButton.Close:
                sys.exit(1)
            case widgets.MessageBox.StandardButton.Ok:
                return True

    # @classmethod
    # def setup_example(cls):
    #     return cls("Title", "Test message")

    @classmethod
    def from_exc(cls, e: Exception, parent=None):
        """Construct message box from a exception."""
        return cls(type(e).__name__, e, parent)

    @classmethod
    def raise_(cls, e: Exception, parent=None):
        """Raise exception in the message box."""
        # unwrap EmitLoopError
        return cls.from_exc(e, parent=parent).exec()

    def _get_traceback(self):
        if self._exc is None:
            return traceback.format_exc()
        formatter = get_tb_formatter(gui.Font.mono().family())
        return formatter(self._exc, as_html=True)

    @classmethod
    def _excepthook(cls, exc_type: type[Exception], exc_value: Exception, exc_traceback):
        """Exception hook used during application execution."""
        strings = traceback.format_exception(exc_type, exc_value, exc_traceback)
        tb = "".join(strings)
        logger.error(tb)
        return ErrorMessageBox.raise_(exc_value, parent=None)

from_exc(e: Exception, parent=None) classmethod

Construct message box from a exception.

Source code in prettyqt/debugging/errormessagebox.py
@classmethod
def from_exc(cls, e: Exception, parent=None):
    """Construct message box from a exception."""
    return cls(type(e).__name__, e, parent)

raise_(e: Exception, parent=None) classmethod

Raise exception in the message box.

Source code in prettyqt/debugging/errormessagebox.py
@classmethod
def raise_(cls, e: Exception, parent=None):
    """Raise exception in the message box."""
    # unwrap EmitLoopError
    return cls.from_exc(e, parent=parent).exec()

:fa-brands-github: Show source on GitHub